package stringTest;
import java.io.UnsupportedEncodingException;
import java.util.regex.Pattern;

import junit.framework.TestCase;


public class strTest extends TestCase {
	
	public void test(String szText, String szKey, int nLength, int nPrev, boolean isNotag, boolean isAdddot){  // 문자???�르�?
	    
	    String r_val = "how to get this file for LG Mobile";
	    szKey = " ";
	    int oF = 0, oL = 0, rF = 0, rL = 0; 
	    int nLengthPrev = 0;
	    Pattern p = Pattern.compile("<(/?)([^<>]*)?>", Pattern.CASE_INSENSITIVE);  // ?�그?�거 ?�턴
	   
	    if(isNotag) {r_val = p.matcher(r_val).replaceAll("");}  // ?�그 ?�거
	    r_val = r_val.replaceAll("&amp;", "&");
	    r_val = r_val.replaceAll("(!/|\r|\n|&nbsp;)", "");  // 공백?�거
	 
	    try {
	      byte[] bytes = r_val.getBytes("UTF-8");     // 바이?�로 보�? 
	      if(szKey != null && !szKey.equals("")) {
	        nLengthPrev = (r_val.indexOf(szKey) == -1)? 0: r_val.indexOf(szKey);  // ?�단 ?�치찾고
	        nLengthPrev = r_val.substring(0, nLengthPrev).getBytes("MS949").length;  // ?�치까�?길이�?byte�??�시 구한??
	        nLengthPrev = (nLengthPrev-nPrev >= 0)? nLengthPrev-nPrev:0;    // �??��?분�???�?��?�도록한??
	      }
	    
	      // x�?�� y길이만큼 ?�라?�다. ?��??�깨�?��.
	      int j = 0;

	      if(nLengthPrev > 0) while(j < bytes.length) {
	        if((bytes[j] & 0x80) != 0) {
	          oF+=2; rF+=3; if(oF+2 > nLengthPrev) {break;} j+=3;
	        } else {if(oF+1 > nLengthPrev) {break;} ++oF; ++rF; ++j;}
	      }
	      
	      j = rF;

	      while(j < bytes.length) {
	        if((bytes[j] & 0x80) != 0) {
	          if(oL+2 > nLength) {break;} oL+=2; rL+=3; j+=3;
	        } else {if(oL+1 > nLength) {break;} ++oL; ++rL; ++j;}
	      }

	      r_val = new String(bytes, rF, rL, "UTF-8");  // charset ?�션
	      System.out.println(r_val);
	      if(isAdddot && rF+rL+3 <= bytes.length) {r_val+="...";}  // ...??붙일�?���??�션
	      
	    } catch(UnsupportedEncodingException e){ e.printStackTrace(); }   
	   
	  
	  }

	/**
	 * @param name
	 */
	public strTest( String name ) {
		super( name );
	}

	/** 
	 * 
	 * @throws java.lang.Exception
	 * @see junit.framework.TestCase#setUp()
	 */
	protected void setUp() throws Exception {
		super.setUp();
	}

	/** 
	 * 
	 * @throws java.lang.Exception
	 * @see junit.framework.TestCase#tearDown()
	 */
	protected void tearDown() throws Exception {
		super.tearDown();
	}


}
